﻿using System;
using System.Linq;
using System.Windows.Forms;
using Seagull.Alerts;
using Seagull.Model;
using Seagull.Model.Alerts;

namespace Seagull.Shell.Windows
{
    public partial class AlertHistoryWindow : Form
    {
        public AlertHistoryWindow()
        {
            InitializeComponent();
        }

        private GenericAlert[] LoadAlerts()
        {
            var genericAlerts = HistoryAlerts.Read(alerts =>
            {
                alerts.Alerts.Reverse();
                return alerts.Alerts.ToArray();
            });
            return genericAlerts;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _alertsListView.Items.Clear();
            _alertsListView.Items.AddRange(
                LoadAlerts()
                    .Select(t => new ListViewItem(new[]
                    {
                        t.Time.ToString("yyyy/MM/dd HH:mm:ss"), 
                        t.AlertContent, 
                        t.AlertDetail
                    }))
                    .ToArray());
        }
    }
}
